<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- el-card 就是一个白底的盒子 -->
      <el-card v-loading="isLoading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <span>{{ company.companyName }}</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex">
              <!-- 两个内容 -->
              <el-col :span="12">负责人</el-col>
              <el-col :span="12">
                <!-- 下拉菜单 element -->
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <!-- 下拉菜单 -->
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addDepts(company)">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 使用的时候 只需要给 el-tree 配置一个data属性 -->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <!--  v-slot:default 可以简写成#default -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <!-- 部门名字 -->
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <!-- 负责人 -->
              <el-col :span="4">
                <el-row type="flex">
                  <!-- 两个内容 -->
                  <el-col :span="12">{{ data.manager }}</el-col>
                  <el-col :span="12">
                    <!-- 下拉菜单 element -->
                    <!-- $event:表示的@command事件的默认参数 必须叫$event -->
                    <!-- 第二个参数 就是要传的额外参数 -->
                    <el-dropdown @command="handleCommand($event, data)">
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          command="add"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          command="edit"
                        >修改部门</el-dropdown-item>
                        <el-dropdown-item
                          command="del"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- show-dialog.sync -->
      <!-- 传过去的值 加一个sync修饰符。 表示 父组件把这个值的修改权 交给组件了 -->
      <addDept
        ref="addDept"
        :show-dialog.sync="showDialog"
        :departs-list="departsList"
        :node-data="nodeData"
      />
    </div>
  </div>
</template>

<script>
import { getDepartmentsListApi, delDepartmentApi } from '@/api/departments'
import { transListToTreeData } from '@/utils'
import addDept from './components/add-dept.vue'
export default {
  name: 'Departments',
  components: {
    addDept
  },
  data() {
    return {
      // 对象类型的数组
      // label: 表示的当前行展示的文本
      // children：表示的当前行有没有子级
      departs: [],
      // 平铺的数据列表
      departsList: [],
      // 公司信息
      company: {},
      defaultProps: {
        // 表示映射关系
        // 使用场景：后端返回字段名不是我们想要的label和children
        label: 'name'
      },
      showDialog: false,
      nodeData: {},
      isLoading: false
    }
  },
  created() {
    this.getDepartmentsList()
  },
  methods: {
    /**
     * 获取部门列表
     */
    async getDepartmentsList() {
      this.isLoading = true
      const { data } = await getDepartmentsListApi()
      this.departs = transListToTreeData(data.depts, '')
      this.departsList = data.depts
      this.company = {
        companyName: data.companyName,
        children: this.departs,
        id: ''
      }
      this.isLoading = false
    },
    /**
     * 点击事件
     */
    handleCommand(cmd, data) {
      if (cmd === 'add') {
        this.addDepts(data)
      }
      if (cmd === 'edit') {
        this.editDepts(data)
      }
      if (cmd === 'del') {
        // 抽离的目的是为了 handleCommand 方法没那么臃肿
        this.delDepts(data)
      }
    },
    /**
     * 修改部门
     *  */
    editDepts(data) {
      this.showDialog = true
      this.nodeData = data
      // this.$refs.addDept.showTitle = '编辑部门'
      // 获取部门负责人的数据
      // this.$refs.addDept.getUserSimpleList()
      this.$nextTick(() => {
        this.$refs.addDept.getDepartmentDetail()
      })
    },
    /**
     * 添加部门
     */
    addDepts(data) {
      this.showDialog = true
      this.nodeData = data
      // 通过父组件调用子组件的方法
      // console.log(this.$refs.addDept)
      // this.$refs.addDept.showTitle = '新增部门'
      // 获取部门负责人的数据
      // this.$refs.addDept.getUserSimpleList()
    },
    closeDialog() {
      this.showDialog = false
    },
    /**
     *删除方法
     */
    delDepts(data) {
      this.$confirm('确定要删除该部门吗？此操作不可逆', '提示')
        .then(async() => {
          // 点击了确定时执行
          await delDepartmentApi(data.id)
          this.$message.success('恭喜！删除成功')
          this.getDepartmentsList()
        })
        .catch(() => {
          // 点击取消时 执行，不配置的话 会报错
        })
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size: 14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url('~@/assets/common/add-circle.png') no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before {
      background: url('~@/assets/common/minus-circle.png') no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before {
      background: url('~@/assets/common/user-filling.png') no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
